/*
    io_com.c - implmentation for io communication driver 
			   This part represents the communication interface (via USB_SERIAL)
	
	
	Author: Benedikt Niedermayr (2013)
	

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

	This source code is also a result of my bachelor thesis.
*/

#include "io_com.h"

/********************************************************************************** IRQ HANDLER
 * -> request irq in init funktion
 * have a look at interrupt documents!!!!
 * 
 */


/**
 * io_serial_check - Check wether ttyUSB* file is aviable.
 * @iop:	global io system data
 *
 * Returns negative errno, or else 0.
 */
int io_serial_check(struct io_com *iop)
{
	return 0;
}



/**
 * io_serial_send - Send data via the ttyUSB* driver.
 * @iop:	global io system data
 *
 * Returns negative errno, or else 0.
 */
int io_serial_send(struct io_com *iop, char *buf, int count)
{
	int ret;
  	mm_segment_t orgfs;

	orgfs=get_fs();
  	set_fs(KERNEL_DS);

	/* open file ttyUSB0 */
	iop->tty_file = filp_open("/dev/ttyUSB0", O_RDWR, 0/*ignored*/);  //O_RDWR | O_NONBLOCK
	set_fs(orgfs);	
	if(iop->tty_file == NULL) {
		printk("Error while getting file descriptor of /dev/ttyUSB0 file\n");
		goto err;
	}
	
	/* Send Command String over serial port */
	if(iop->tty_file->f_op->write) {

		/* Start write operation of ttyUSB file */
		set_fs(KERNEL_DS);
		ret = iop->tty_file->f_op->write(iop->tty_file, buf, count, &iop->tty_file->f_pos);	
		set_fs(orgfs);	
	} else {

		printk("Write operations not supported in fops!\n");	
		goto err;
	}
	filp_close(iop->tty_file, NULL);
	return 0;

err:
	filp_close(iop->tty_file, NULL);
	return -EIO;
}


/**
 * io_serial_recv - Receive data via the ttyUSB* driver.
 * @iop:	global io system data
 *
 * Returns negative errno, or else 0.
 */
int io_serial_recv(struct io_com *iop, char *buf, int count)
{
	return 0;
}


static int io_com_probe(struct io_com *iop)
{
	printk("Starting io_com_probe function\n");

	/* Check I/O extension functionality.
     * First communication with the slave.
     * The buffer to be sent has not to be saved via mutex
     * because this function will be called only one time!
     */
	
	/* specifiy IRQ Number */
	/***********************/


	/* register an Interrupt */
	/*************************/

	return 0;
		
}


static int __init io_com_init(void)
{	
	
	if(global_pointer == NULL)
		return -ENODEV;

	/* Set recv and send handler functions */
	global_pointer->io_handler_send = io_serial_send;
	global_pointer->io_handler_recv = io_serial_recv; 


	/* Start own implementation of probe function */
	io_com_probe(global_pointer);
	return 0;
}

static void __exit io_com_exit(void)
{
	if(global_pointer == NULL)
		return;
		
}

module_init( io_com_init );
module_exit( io_com_exit );

MODULE_AUTHOR("Benedikt Niedermayr");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("i/o controller usb-serial communication interface");
MODULE_SUPPORTED_DEVICE("none");


